<template>
  <div class="dashboard-container">
    <div class="app-container">

      <el-card class="tree-card">
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <TreeTools slot-scope="{ data }" :tree-node="data" :is-root="false" @addDepts="addDepts" @delDepts="getDepartments" @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <addDept ref="add-depts" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'
import addDept from './components/add-dept.vue'
export default {
  name: 'Treetools',
  components: {
    TreeTools,
    addDept
  },
  data() {
    return {
      company: {},
      departs: [],
      node: null,
      defaultProps: {
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      console.log(this.company)
      // this.departs = result.depts
      this.departs = tranListToTreeData(result.depts, '')
      console.log(result)
    },
    addDepts(val) {
      console.log(val, '1111')
      console.log('开')
      console.log(this.$refs['add-depts'])
      this.node = val

      this.$refs['add-depts'].isShow = true
    },
    // 修改
    async editDepts(val) {
      this.node = val
      this.$refs['add-depts'].isShow = true
      await this.$refs['add-depts'].getDepartDetail(val.id)
    }
  }
}
</script>

